#if UNITY_EDITOR
using GameCore;
using Net.Helper;
using Net.Unity;
using UnityEditor;
using UnityEngine;

namespace GameCore
{
    [CustomEditor(typeof(ABPrefabCollection), editorForChildClasses: true)]
    [CanEditMultipleObjects]
    public class ABPrefabCollectionEditor : Editor
    {
        private ABPrefabCollection prefabCollection;

        private void OnEnable()
        {
            prefabCollection = target as ABPrefabCollection;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            if (GUILayout.Button("收集网络预制体"))
            {
                prefabCollection.prefabInfos.Clear();
                var guids = AssetDatabase.FindAssets("t:Prefab");
                foreach (string guid in guids)
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);
                    var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                    var components = prefab.GetComponents<MonoBehaviour>();
                    foreach (var component in components)
                    {
                        if (component == null)
                            continue;
                        var typeName = component.GetType().ToString();
                        if (typeName == "Net.UnityComponent.NetworkObject" || typeName == "ActorSystem.ActorView")
                        {
                            prefabCollection.prefabInfos.Add(new PrefabInfo()
                            {
                                prefabPath = path,
                                prefabHash = path.CRC32(),
                                prefab = prefab,
                            });
                            break;
                        }
                    }
                }
                EditorUtility.SetDirty(prefabCollection);
            }
        }
    }
}
#endif